/*
 * This represents a single level in the game.
 * This class is also able to load a level from disk.
 */

package wallythewatermelon;

import java.awt.Graphics;
import java.awt.Image;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.Scanner;
import javax.imageio.ImageIO;

/**
 *
 * @author mgohde
 */
public class Level 
{
    public static Button_Pause pausebutton = new Button_Pause(WallyTheWatermelon.WINDOW_WIDTH-32, 0);
    
    public Sprite levelData[][];
    public Block blocksArray[][];
    
    public Sprite endPoint;
    
    public static final int LEVEL_WIDTH=16;
    public static final int LEVEL_HEIGHT=8;
    public static final char BLOCK='B';
    public static final char PLAYERSTART='P';
    public static final char ENDPOINT='E';
    public static final char ENEMY_BROC='R';
    public static final char ENEMY_POTATO='O';
    public static final char ENEMY_LETTUCE='L';
    public static final char ENEMY_CARROT='C';
    public static final char EMPTY_BLOCK=' ';
    
    public static final String BLOCK_IMAGENAME="Grass";
    public static final String ENDPOINT_IMAGENAME="tina";
    public static final String ENEMY_BROC_IMAGENAME="Broccoli";
    
    public Image blockImageBackup;
    public Image endpointImageBackup;
    public Image enemyBrocImageBackup;
    
    Watermelon wally = new Watermelon(10, WallyTheWatermelon.WINDOW_HEIGHT-(128-20));
    public int tempbcount=0;
    public int bcount=0;
    
    public void readImages() throws IOException
    {
        blockImageBackup=ImageIO.read(new File("textures/"+BLOCK_IMAGENAME+".png"));
        endpointImageBackup=ImageIO.read(new File("textures/"+ENDPOINT_IMAGENAME+".png"));
        enemyBrocImageBackup=ImageIO.read(new File("textures/"+ENEMY_BROC_IMAGENAME+".png"));
    }
    
    public void loadLevel(String fileName)
    {
        File file=new File("levels/"+fileName+".lvl");
        int i, j;
        String inStr;
        
        blocksArray=new Block[LEVEL_WIDTH][LEVEL_HEIGHT];
        
        levelData=new Sprite[LEVEL_WIDTH][LEVEL_HEIGHT];
        try {
            readImages();
            Scanner fileReader=new Scanner(file);
            
            for(i=0;i<8;i++)
            {
                inStr=fileReader.nextLine();
                
                for(j=0;j<16;j++)
                {
                    //System.out.println(inStr);
                    levelData[j][i]=new Sprite();
                    levelData[j][i].y=i*64;
                    levelData[j][i].x=j*64;
                    levelData[j][i].blockType=inStr.charAt(j);
                    if(inStr.charAt(j)==BLOCK)
                    {
                        blocksArray[j][i]=new Block(j*64, i*64, blockImageBackup);
                        blocksArray[j][i].blockType=inStr.charAt(j);
                    }
                    
                    else
                    {
                        blocksArray[j][i]=null;
                    }
                    
                    if(inStr.charAt(j)==ENDPOINT)
                    {
                        endPoint=levelData[j][i];
                    }
                }
            }
        } catch (FileNotFoundException ex) {
            System.err.println("ERR: COULD NOT FIND LEVEL "+fileName);
        } catch (IOException e){
            System.err.println("ERR: COULD NOT LOAD IMAGES.");
        }
    }
    
    public void draw(Graphics surface) 
    {
        int i, j;
        wally.draw(surface);
        pausebutton.draw(surface);
        
        for(i=0;i<LEVEL_HEIGHT;i++)
        {
            for(j=0;j<LEVEL_WIDTH;j++)
            {
                switch(levelData[j][i].blockType)
                {
                    case BLOCK:
                        Block blk=new Block(j*64, i*64, blockImageBackup);
                        blk.draw(surface);
                        tempbcount++;
                        break;
                    case ENDPOINT:
                        Block endPoint=new Block(j*64, i*64,endpointImageBackup);
                        endPoint.draw(surface);
                        break;
                    case ENEMY_BROC:
                        EnemyBroc broc=new EnemyBroc(j*64, i*64, enemyBrocImageBackup);//(EnemyBroc) levelData[j][i];
                        broc.draw(surface);
                        break;
                }
            }
        }
        bcount = tempbcount;
        tempbcount = 0;
    }
}
